package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.data.manager.IProductData;
import cc.iotkit.data.miniAdmin.IDeviceDistributorData;
import cc.iotkit.data.miniAdmin.IDeviceDistributorDetailData;
import cc.iotkit.data.miniAdmin.IGoodsData;
import cc.iotkit.manager.dto.vo.miniAdmin.*;
import cc.iotkit.manager.service.IDeviceDistributorService;
import cc.iotkit.model.miniAdmin.*;
import cc.iotkit.model.product.Product;
import cn.hutool.core.bean.BeanUtil;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class DeviceDistributorServiceImpl implements IDeviceDistributorService {

    private final IDeviceDistributorData deviceDistributorData;

    private final IDeviceDistributorDetailData deviceDistributorDetailData;

    private final IProductData productData;

    @Override
    public Paging<MiniAdminDeviceDistributorVo> getDevices(PageRequest<String> pageRequest) {
        Paging pageing = new Paging<>();

        pageing = deviceDistributorData.findByConditions(pageRequest);

        List<DeviceDistributorDTO> list =  pageing.getRows();
        List<MiniAdminDeviceDistributorVo> distributorVos = new ArrayList<>();

        for (DeviceDistributorDTO item:list){
            MiniAdminDeviceDistributorVo distributorVo  = new MiniAdminDeviceDistributorVo();
            BeanUtils.copyProperties(item,distributorVo);
            distributorVos.add(distributorVo);
        }
        pageing.setRows(distributorVos);
        return  pageing;
    }




    @Override
    public MiniAdminDeviceDistributorDetailVo getDetail(Long bindId) {
        MiniAdminDeviceDistributorDetailVo detailVo = new MiniAdminDeviceDistributorDetailVo();
        DeviceDistributorDetailDTO4Page detailDTO = deviceDistributorData.getDetail(bindId);

        BeanUtil.copyProperties(detailDTO,detailVo);

        List<DeviceDistributorChannelDTO> channelDTOS = detailDTO.getChannelList();
        List<MiniAdminDeviceDistributorChannelVo> channelVoList = new ArrayList<>();
        for(DeviceDistributorChannelDTO channelDTO:channelDTOS){

            MiniAdminDeviceDistributorChannelVo channelVo = new MiniAdminDeviceDistributorChannelVo();

            channelVo.setId(channelDTO.getId());
            channelVo.setDeviceId(channelDTO.getDeviceId());
            channelVo.setCount(channelDTO.getCount());
            channelVo.setName(channelDTO.getName());
            channelVo.setLabel(channelDTO.getLabel());
            channelVo.setGoodsClassify(channelDTO.getGoodsClassify());
            channelVo.setGoodsId(channelDTO.getGoodsId());
            channelVo.setBatteryRecognition(channelDTO.getBatteryRecognition());
            channelVo.setDroneRecognition(channelDTO.getDroneRecognition());
            String batterList = channelDTO.getBatteryList();

            ObjectMapper objectMapper = new ObjectMapper();
            if(StringUtils.isNotEmpty(batterList)){

                if (StringUtils.isNotEmpty(batterList)) {
                    try {
                        // 使用 TypeReference 保留泛型信息
                        List<MiniAdminDeviceDistributorChannelBatteryVo> batteryList = objectMapper.readValue(
                                batterList, new TypeReference<List<MiniAdminDeviceDistributorChannelBatteryVo>>() {});

                        channelVo.setBatteryList(batteryList);
                    } catch (IOException e) {
                        // 异常处理
                        e.printStackTrace();
                        // 或者抛出自定义异常
                        //throw new IllegalArgumentException("Invalid JSON format for battery list", e);
                    }
                }
            }
            channelVoList.add(channelVo);
        }

        //detailVo.setChannelVoList(channelVoList);


        return detailVo;
    }

    @Override
    public void registerDevice(MiniAdminDeviceDistributorVo deviceDistributorVo) {
        DeviceDistributorDetailDTO4Page deviceDistributorDetailDTO = new DeviceDistributorDetailDTO4Page();

        DeviceDistributor deviceDistributor = new DeviceDistributor();
        BeanUtils.copyProperties(deviceDistributorVo, deviceDistributor);
        deviceDistributorData.save(deviceDistributor);

        //BeanUtils.copyProperties(deviceDistributorVo, deviceDistributorDetailDTO);

        //List<MiniAdminDeviceDistributorChannelVo> channelVoList = deviceDistributorVo.getChannelVoList();

       // List<DeviceDistributorChannelDTO> channelDTOS = new ArrayList<>();
                //BeanUtil.copyToList(channelVoList,DeviceDistributorChannelDTO.class);

//        ObjectMapper mapper = new ObjectMapper();
//        for(MiniAdminDeviceDistributorChannelVo channelVo:channelVoList){
//            DeviceDistributorChannelDTO channelDTO = new DeviceDistributorChannelDTO();
//            BeanUtil.copyProperties(channelVo,channelDTO);
//            List<MiniAdminDeviceDistributorChannelBatteryVo> batteryVos = channelVo.getBatteryList();
//            try {
//                String str = mapper.writeValueAsString(batteryVos);
//                channelDTO.setBatteryList(str);
//            }catch (Exception e){}
//
//            channelDTOS.add(channelDTO);
//        }
        //deviceDistributorDetailDTO.setChannelList(channelDTOS);

        //deviceDistributorData.add(deviceDistributorDetailDTO);


    }

    @Override
    public void updateDevice(MiniAdminDeviceDistributorVo deviceDistributorVo) {
        //DeviceDistributorDetailDTO4Page deviceDistributorDetailDTO = new DeviceDistributorDetailDTO4Page();
        DeviceDistributor deviceDistributor = new DeviceDistributor();
        BeanUtils.copyProperties(deviceDistributorVo, deviceDistributor);
        //List<MiniAdminDeviceDistributorChannelVo> channelVoList = deviceDistributorVo.getChannelVoList();
//        List<DeviceDistributorChannelDTO> channelDTOS =
//                BeanUtil.copyToList(channelVoList,DeviceDistributorChannelDTO.class);
//        deviceDistributorDetailDTO.setChannelList(channelDTOS);

        deviceDistributorData.save(deviceDistributor);
                //update(deviceDistributorDetailDTO);
    }

    @Override
    public void unbindDevice(MiniAdminUnbindDeviceDistributorVo data) {
        deviceDistributorData.delete(data.getBindId());

    }

    @Override
    public void testElectric(MiniAdminDeviceTestVo data) {

    }

    @Override
    public void test(MiniAdminDeviceTestVo deviceTestVo) {

    }


    @Override
    public List<MiniAdminDeviceDistributorChannelVo> getChannelList(Long bindId) {

        DeviceDistributorDetailDTO4Page detailDTO = deviceDistributorData.getDetail(bindId);
        List<DeviceDistributorChannelDTO> channelDTOS =  detailDTO.getChannelList();
        List<MiniAdminDeviceDistributorChannelVo> channelVoList = new ArrayList<>();

        for(DeviceDistributorChannelDTO channelDTO:channelDTOS){

            MiniAdminDeviceDistributorChannelVo channelVo = new MiniAdminDeviceDistributorChannelVo();

            channelVo.setId(channelDTO.getId());
            channelVo.setDeviceId(channelDTO.getDeviceId());
            channelVo.setCount(channelDTO.getCount());
            channelVo.setName(channelDTO.getName());
            channelVo.setLabel(channelDTO.getLabel());
            channelVo.setGoodsClassify(channelDTO.getGoodsClassify());
            channelVo.setGoodsId(channelDTO.getGoodsId());
            channelVo.setBatteryRecognition(channelDTO.getBatteryRecognition());
            channelVo.setDroneRecognition(channelDTO.getDroneRecognition());

            Product product = productData.findById(channelDTO.getGoodsId());
            channelVo.setCostPrice(product.getCostPrice());
            channelVo.setSalePrice(product.getSalePrice());
            channelVo.setPromotePrice(product.getPromotePrice());

            channelVoList.add(channelVo);
        }
        return channelVoList;
    }


    @Override
    public void saveChannelList(List<MiniAdminDeviceDistributorChannelVo> channelVoList) {
        List<DeviceDistributorDetail> details = new ArrayList<>();
        for(MiniAdminDeviceDistributorChannelVo channelVo:channelVoList){
            DeviceDistributorDetail distributorDetail = new DeviceDistributorDetail();
            distributorDetail.setGoodsId(channelVo.getGoodsId());
            distributorDetail.setGoodsClassify(channelVo.getGoodsClassify());

            distributorDetail.setBindId(channelVo.getBindId());

            details.add(distributorDetail);
        }
        deviceDistributorDetailData.batchSave(details);
    }

    @Override
    public void resetChannelList(Long bindId) {
        deviceDistributorDetailData.resetChannelList(bindId);
    }
}
